define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Pbuy, GoPage, $ionicSlideBoxDelegate,GoDialog,$timeout) {

		$scope.input = new Object();
		$scope.input.keywords = "";

		//来回切换
	   $scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			$scope.init_data();//必须要，因为这里用$scope.selectTab作为了初始化参数
		};
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		

		//页面跳转
		$scope.onshow_mypbuy_edit = function(id){
			GoPage.go_states("mypbuy_edit",{"id":id});
		}

		//加载数据
		$scope.order = 1;
		$scope.init_data = function() {
			Pbuy.find_goods($scope.input.keywords,1,$scope.order,$scope.selectTab,function(data) {
				$scope.pbuylist = data;
			});
		}
		$scope.init_data();//初始化

		//按时间排序
		$scope.orderBy_time = function(){
			$scope.input.nPage = 1;
			if($scope.order==1){
				$scope.order = 2;
			}else{
				$scope.order=1;
			}
			$scope.init_data();
		}

		//删除
		$scope.deleteOrder = function(id){
			Pbuy.deleteOrder(id,function(data) {
			    console.log(data)
				if(data.success){
					GoDialog.toast("删除成功！");
					$scope.deleteItem(id);
				}else{
					GoDialog.toast(data.msg);
				}
			});
		}
		//删除现有数组里的数据
		$scope.deleteItem = function(id){
			var index = -1;
			for (var i=0;i<$scope.pbuylist.findgoods.length;i++) {
				if($scope.pbuylist.findgoods[i].id==id){
					index = i;
					break;
				}
			}
			$scope.pbuylist.findgoods.splice(index,1);//删除数组里的1个元素
		}

        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
           Pbuy.find_goods($scope.input.keywords,1,$scope.order,$scope.selectTab,function(data) {
                    if(data != null) {
                       $scope.pbuylist = data;

                    }else{
                    	$scope.pbuylist = new Object();
                    }
                    $scope.input.nPage = 1;
                },
                function() {
                    $scope.$broadcast('scroll.refreshComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            Pbuy.find_goods($scope.input.keywords,$scope.input.nPage +1,$scope.order,$scope.selectTab,function(data)  {
                    if(data != null) {
                        if($scope.pbuylist.findgoods != undefined) {
                            $scope.pbuylist.findgoods = $scope.pbuylist.findgoods.concat(data.findgoods);
                            $scope.input.nPage += 1;
                        }else {
                            $scope.pbuylist = data;
                        }
                    }
                },
                function() {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);

                });

        };


    }

    controller.$inject = ['$scope', 'Pbuy', 'GoPage', '$ionicSlideBoxDelegate','GoDialog','$timeout'];

    return app.registerController("pbuyctrl",controller);
});